package com.ecloud.gardensterminal.server.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.gardensterminal.constant.DeviceTypeConstant;
import com.ecloud.gardensterminal.controller.dto.GardensDeviceSelectDto;
import com.ecloud.gardensterminal.controller.vo.GardensTodayTemperatureDifferenceVo;
import com.ecloud.gardensterminal.dao.GardensDeviceMapper;
import com.ecloud.gardensterminal.dao.GardensWeatherMapper;
import com.ecloud.gardensterminal.domain.GardensDevice;
import com.ecloud.gardensterminal.domain.GardensWeather;
import com.ecloud.gardensterminal.server.GardensWeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Squbi
 */
@Service
public class GardensWeatherServiceImpl extends ServiceImpl<GardensWeatherMapper, GardensWeather> implements GardensWeatherService {
    @Autowired
    GardensWeatherMapper weatherMapper;

    @Autowired
    GardensDeviceMapper deviceMapper;

    @Override
    public List<GardensWeather> selectList(GardensDeviceSelectDto dto) {
        dto.setPageIndex(dto.getPageSize() * (dto.getPageIndex() - 1));
        return weatherMapper.selectPageByDto(dto);
    }

    @Override
    public List<GardensWeather> selectNewly() {
        List<GardensDevice> gardensDevices = deviceMapper.selectByDeviceType(DeviceTypeConstant.WEATHER);
        List<GardensWeather> result = new ArrayList<>();
        for (GardensDevice gardensDevice : gardensDevices) {
            List<GardensWeather> gardensSleets = weatherMapper.selectPageByDto(new GardensDeviceSelectDto(gardensDevice.getDeviceId(), 1, 1));
            result.addAll(gardensSleets);
        }
        return result;
    }

    @Override
    public GardensTodayTemperatureDifferenceVo gardensTodayTemperatureDifference() {
        Double max = weatherMapper.currentDateTemperatureMax();
        Double min = weatherMapper.currentDateTemperatureMin();
        return new GardensTodayTemperatureDifferenceVo(max, min);
    }
}
